//
//  MWRegisterController.m
//  XiRan-iOS
//
//  Created by Richfit on 2019/10/21.
//  Copyright © 2019 Wood. All rights reserved.
//

#import "MWRegisterController.h"
#import "MWRegisterPhoneController.h"
#import "MWRegisterEmailController.h"
#import "MWTabBarController.h"

#import "CQCountDownButton.h"
#import "JKCountDownButton.h"

#import "MWUser.h"

@interface MWRegisterController () <WMPageControllerDataSource, WMPageControllerDelegate>

@property(nonatomic, strong) UIView *topMenuView;
@property(nonatomic, strong) UIButton *backBtn;
@property(nonatomic, strong) UILabel *hintLB;

@property(nonatomic, strong) WMPageController *pageController;

@property(nonatomic, strong) NSMutableArray *categoryArray;
@property(nonatomic, strong) NSMutableArray *controllerArray;

@end

@implementation MWRegisterController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupPageSubviews];
    [self layoutPageSubviews];
    [self setLocalizable];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

- (void)backAction:(UIButton *)sender {
    [self.view endEditing:YES];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - WMPageControllerDataSource && WMPageControllerDelegate
- (NSInteger)numbersOfChildControllersInPageController:(WMPageController *)pageController {
    return 2;
}

- (NSString *)pageController:(WMPageController *)pageController titleAtIndex:(NSInteger)index {
    if (index == 0) {
        return [[MWConfigManager manager] objectForKey:@"email_register" defaultValue:@"邮箱注册"];
    } else {
        return [[MWConfigManager manager] objectForKey:@"mobile_register" defaultValue:@"手机号注册"];
    }
}

- (UIViewController *)pageController:(WMPageController *)pageController viewControllerAtIndex:(NSInteger)index {
    return self.controllerArray[index];
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForMenuView:(WMMenuView *)menuView {
    menuView.scrollView.backgroundColor = MWHexColor(@"#ECF9FF");
    return CGRectMake(0, kStatusBarHeight + 44 + 66 + 30 + 40, kScreenWidth, 44);
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForContentView:(WMScrollView *)contentView {
    return CGRectMake(0, kStatusBarHeight + 44 + 66 + 30 + 44 + 40, kScreenWidth, (kScreenHeight - kStatusBarHeight + 44 + 66 + 30 + 44  + 40));
}

- (NSMutableArray *)categoryArray {
    if (!_categoryArray) {
        _categoryArray = [[NSMutableArray alloc] init];
    }
    return _categoryArray;
}

- (NSMutableArray *)controllerArray {
    if (!_controllerArray) {
        _controllerArray = [[NSMutableArray alloc] init];
    }
    return _controllerArray;
}


#pragma mark - 国际化
- (void)setLocalizable {
    if ([[MWConfigManager manager].productType isEqualToString:@"0"]) {
        _hintLB.text = [[MWConfigManager manager] objectForKey:@"welcome_into_wood" defaultValue:@"欢迎来到我的木头"];
    } else {
        _hintLB.text = [[MWConfigManager manager] objectForKey:@"welcome_into_xiran" defaultValue:@"欢迎来到木头猎人"];
    }
}

#pragma mark - UI
- (void)setupPageSubviews {
    MWRegisterEmailController *emailController = [[MWRegisterEmailController alloc] init];
    MWRegisterPhoneController *phoneController = [[MWRegisterPhoneController alloc] init];
    [self.controllerArray addObject:emailController];
    [self.controllerArray addObject:phoneController];
    
    _pageController = [[WMPageController alloc] init];
    _pageController.dataSource = self;
    _pageController.delegate = self;

    _pageController.menuViewLayoutMode = WMMenuViewLayoutModeLeft;
    _pageController.menuViewStyle = WMMenuViewStyleLine;
    _pageController.pageAnimatable = YES;
    _pageController.automaticallyCalculatesItemWidths = YES;
    _pageController.itemMargin = 15;
    _pageController.progressWidth = 30;
    _pageController.progressHeight = 2;
    _pageController.progressColor = MWHexColor(@"#1E1934");
    _pageController.titleFontName = @"PingFangSC-Semibold";
    _pageController.titleSizeNormal = 15;
    _pageController.titleSizeSelected = 16;
    _pageController.titleColorNormal   = MWHexColor(@"#999999");
    _pageController.titleColorSelected = MWHexColor(@"#1E1934");
    _pageController.menuView.scrollView.backgroundColor = MWHexColor(@"#ECF9FF");
    _pageController.scrollView.backgroundColor = MWHexColor(@"#21BDFF");
    [self addChildViewController:_pageController];
    [self.view addSubview:_pageController.view];
    
    _topMenuView = [[UIView alloc] init];
    _topMenuView.backgroundColor = MWHexColor(@"#ECF9FF");
    [self.view addSubview:_topMenuView];
    
    _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_backBtn setImage:[UIImage imageNamed:@"ic_arrow_left"] forState:UIControlStateNormal];
    [_backBtn setImage:[UIImage imageNamed:@"ic_arrow_left"] forState:UIControlStateHighlighted];
    [_backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [_topMenuView addSubview:_backBtn];
       
    _hintLB = [[UILabel alloc] init];
    _hintLB.font = MWBoldFont(22);
    _hintLB.textColor = MWHexColor(@"#1E1934");
    [_topMenuView addSubview:_hintLB];
}

- (void)layoutPageSubviews {
    [_topMenuView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
    }];
    
    [_backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topMenuView).offset(kStatusBarHeight);
        make.left.equalTo(@16);
        make.height.equalTo(@44);
    }];
    
    [_hintLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backBtn.mas_bottom).offset(66);
        make.left.equalTo(self.view).offset(16);
        make.height.equalTo(@30);
        make.bottom.equalTo(self.topMenuView).offset(-40);
    }];
}

@end
